package repository.artefact
import  repository.artefact.details.*


class ArtefactService {
    static scope = 'prototype'
    static transactional = false
    String relativePath
    String extension
    
    def JerarquiaDetallesArtefacto getJerarquiaDetallesArtefacto() {
        NodoDetallesArtefacto nodoDetallesArtefacto = getNodoDetallesArtefacto(relativePath)
        JerarquiaDetallesArtefacto jerarquia = new  JerarquiaDetallesArtefacto()
        
        jerarquia.detallesArtefacto = nodoDetallesArtefacto
                                             
        return jerarquia
    }
    
    def Artefacto get(String n, String v, String p, Artefacto artefacto) {        
        def path = p.replaceAll("\\+","/").replaceAll(" ","/")
        String fullName = relativePath + "/" + path + "/" + n + ".v" + v + extension
        File file = new File(fullName)
        if(file.exists()){               
            artefacto.contenido = file.getText().encodeAsBase64().toString()
            DetallesArtefacto detalleArtefacto = this.getDetallesArtefacto(file)
            if(detalleArtefacto != null){
                artefacto.detalleArtefacto = detalleArtefacto
            }
            
            return artefacto
        }  else {
            return null
        }               
    }
    
    def boolean save(Artefacto artefacto){
        boolean result = false
        
        String path = artefacto.detalleArtefacto.path.replaceAll("\\+","/")
        String n =  artefacto.detalleArtefacto.nombreArchivo
        String v = artefacto.detalleArtefacto.numVersion
        String fullName = relativePath + "/" + path + "/" + n + ".v" + v + extension
        File file = new File(fullName)
        
        if(file.exists()){
            file.delete()
        } else {
            String dirPath = file.parent
            File dir = new File(dirPath)
            if(!dir.exists()){
                dir.mkdirs()
            }
        }
                            
        if(file.createNewFile()){
            String decode = new String(artefacto.contenido.decodeBase64())
            file.append(decode)
            result = true
        }
        
        return result
    }
    
    
    /************** Auxiliares ***************/  
    private NodoDetallesArtefacto getNodoDetallesArtefacto(String path){        
        def file = new File(path)        
        if(file.exists()){                                 
            if(file.isDirectory()){  
                NodoDetallesArtefacto d = new NodoDetallesArtefacto()            
                d.nombre = file.name
                
                       
                file.eachDir { dir -> 
                    def hijo = this.getNodoDetallesArtefacto(dir.path)
                    d.nodoHijo.add(hijo)                      
                }                                                                 
                               
                file.eachFile { f ->          
                    if(f.name ==~ (/.+\..+\.v\d+(draft)?/ + extension)){
                        DetallesArtefacto detallesArtefacto = this.getDetallesArtefacto(f)
                        if(detallesArtefacto != null){
                            d.detallesArtefactos.add(detallesArtefacto)    
                        }
                    }
                }
                                
                return d
            } 
        }
    }
    
    private Map splitNameFile(String fileName){
        String [] strs = fileName.split("\\.")
        def attrs = null
        
        if(strs != null && strs.size() == 4){
            attrs = [prefix:strs[0],name:strs[1],version:strs[2],extension:strs[3]]
        }                    
        
        return attrs
    }
    
    private DetallesArtefacto getDetallesArtefacto(File f){
        DetallesArtefacto detallesArtefacto = null
        def attrs = splitNameFile(f.name)
        
        
        if(attrs != null){
            detallesArtefacto = new DetallesArtefacto()
            detallesArtefacto.nombre = attrs.name                    
            detallesArtefacto.path = f.parent.substring(relativePath.size()+1).replaceAll("\\\\","\\+")            
            detallesArtefacto.nombreArchivo = attrs.prefix + "." + attrs.name
            detallesArtefacto.numVersion = attrs.version != null && attrs.version[0] == 'v' ? attrs.version.substring(1) : attrs.version             
        }
        
        return detallesArtefacto
    }
}
